<?php

include_once 'Super_Model_Admin.php';

class Admin_Model_Labels extends Super_Model_Admin {

    public function getAllLabels() {
        $result = $this->select('labels', '`id`,`title`', '', '`title`');
        if ($result) {
            return $result;
        }
        return false;
    }

    public function getLabelsByPage($page_id) {
        $result = array();
        $labels = $this->getAttach('page', $page_id, 'label');
        if(isset ($labels[0]['target_id'])){
            foreach ($labels as $label)
                $result[] = $label['target_id'];
        }
        return $result;
    }

    public function saveLabels($page_id, $lbl){
        if (!empty($page_id)) {
            $this->removeAttach('page', $page_id, 'label');
            if (!empty($lbl)) {
                $labels = explode('|', $lbl);
                if (is_array($labels) && count($labels)) {
                    foreach ($labels as $label)
                        $this->insertAttach('page', $page_id, 'label', $label);
                    return true;
                }
            }
        } else {
            $this->error(__METHOD__ . ' empty parameters');
        }
    }
}